package com._classloader;

import java.io.*;

/**
 * @Author: huangya
 * @Date: 2019/9/10
 * @Description:
 */
public class MyClassLoader extends ClassLoader {

    @Override
    protected Class<?> findClass(String className) throws ClassNotFoundException {
        try {
            InputStream fis = this.getResourceAsStream(className.replace('.', File.separatorChar) + ".class");;
            byte[] classBytes = new byte[fis.available()];
            fis.read(classBytes);
            return defineClass(className, classBytes, 0, classBytes.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
//        Class.forName("zzzzz_just_for_test.DDD");

        MyClassLoader classLoader = new MyClassLoader();
        Class clazz = classLoader.findClass("zzzzz_just_for_test.DDD");
//        clazz.newInstance();
//        System.out.println(clazz);

        Class clazz222 = Thread.currentThread().getContextClassLoader().loadClass("zzzzz_just_for_test.DDD");


    }

}
